package com.app.utils.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;

/**
 * Gson解析帮助类
 * Created by xuxinghai on 2016/6/14.
 */
public class GsonUtils {

    private static GsonUtils mGsonUtils;//唯一实列

    private static Gson mGson;

    private GsonUtils(){}

    /**
     * 获取唯一实例
     * @return
     */
    public static GsonUtils getInstance(){
        if(mGsonUtils == null){
            mGsonUtils = new GsonUtils();
        }
        return mGsonUtils;
    }

    /**
     * 获取一个解析器
     * @return Gson
     */
    public Gson getGson(){
        if(mGson == null){
            mGson = new Gson();
        }
        return mGson;
    }

    /**
     * 返回一个新的Gson解析器
     * @param jsonClass 需要解析的类
     * @param jsonDeserializer 自定义解析器
     * @return Gson
     */
    public Gson getCustomGson(Class jsonClass,JsonDeserializer<?> jsonDeserializer){
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(jsonClass, jsonDeserializer)
                .create();
        return gson;
    }

}
